﻿using System;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using MDCAdminTool.Controls;
using System.ComponentModel;
using MDCAdminTool.Reports;

namespace MDCAdminTool.Commands
{
    public abstract class ReportCommandBindingBase : AsyncCommandBindingBase
    {
        protected TabControl TabControl;
        protected TabControl InitTabControl()
        {
            if ( TabControl == null )
            {
                TabControl = Application.Current.Properties["TabControl"] as TabControl;
            }
            return TabControl;
        }
        protected virtual void  AddNewReport(string header , Type chartType, params Object[] args)
        {
            if( InitTabControl() == null ) return;

            try
            {
                TabControl.Dispatcher.Invoke(
                    new Action(
                        () =>
                        {
                            var chart  = Activator.CreateInstance( chartType , args );
                            var report = new TabItem { Header = new HeaderChart { Title = header } , Content = chart };
                            TabControl.Items.Add( report );
                            TabControl.SelectedItem = report;
                        }
                     ) );
            }
            catch {}
        }

        protected ReportCommandBindingBase()
        {
            TabControl = Application.Current.Properties["TabControl"] as TabControl;
        }
    }

    public class ClusterSize : ReportCommandBindingBase
    {
        public ClusterSize()
        {
            Command    = Application.Current.Resources["ClusterSize"] as ICommand;
        }

        protected override void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
        protected override void OnExecuted( object sender , ExecutedRoutedEventArgs e )
        {
            var cache = e.Parameter as CacheHost;
            if ( cache == null )
            {
                var clusters = Application.Current.Properties["Clusters"] as Clusters;

                if ( clusters != null && clusters[0] != null ) 
                    cache = clusters[0].Hosts[0];
            }
            if ( cache != null )
            {
                foreach (var c in cache.Cluster.Hosts )
                {
                    c.LoadStatistics();
                }

                AddNewReport( "Cluster Size Report" , typeof(ClusterSizeChart ),  cache  );
            }
        }
    }
    public class CacheNameSizesByHost : ReportCommandBindingBase
    {
        public CacheNameSizesByHost()
        {
            Command    = Application.Current.Resources["CacheNameSizesByHost"] as ICommand;
        }

        protected override void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
        protected override void OnExecuted( object sender , ExecutedRoutedEventArgs e )
        {
            var cache = e.Parameter as CacheHost;
            if ( cache != null )
            {
                foreach ( var c in cache.Cluster.Hosts )
                {
                    c.LoadStatistics();
                }

                AddNewReport( 
                    "CacheName Sizes By Host" , 
                    typeof(CacheNameSizesByHostChart) , 
                    cache );
            }
        }
    }
    public class CacheNameStats : ReportCommandBindingBase
    {
        public CacheNameStats()
        {
            Command = Application.Current.Resources["CacheNameStats"] as ICommand;
        }

        protected override void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
        protected override void OnExecuted( object sender , ExecutedRoutedEventArgs e )
        {
            var cache = e.Parameter as MdcCache;
            if ( cache != null )
            {
                cache.LoadStatistics();

                AddNewReport(
                    "CacheName Stats" ,
                    typeof( CacheNameStatsChart ) ,
                    cache );
            }
        }
    }
    public class CacheHostStats : ReportCommandBindingBase
    {
        public CacheHostStats()
        {
            Command = Application.Current.Resources["CacheHostStats"] as ICommand;
        }

        protected override void OnCanExecute( object sender , CanExecuteRoutedEventArgs e )
        {
            e.CanExecute = true;
        }
        protected override void OnExecuted( object sender , ExecutedRoutedEventArgs e )
        {
            var cache = e.Parameter as CacheHost;
            if ( cache != null )
            {
                cache.LoadStatistics();

                AddNewReport(
                    "CacheHost Stats" ,
                    typeof( CacheHostStatsChart ) ,
                    cache );
            }
        }
    }
}
